﻿using Common;
using DbUtility;
using STMS.Models.DModels;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STMS.DAL
{
    public class ProductDAL : BaseDAL<ProductInfo>
    {
        /// <summary>
        /// 条件查询产品列表
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="lowTemper"></param>
        /// <param name="highTemper"></param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public List<ProductInfo> GetProductList(string keywords, int isDeleted)
        {
            List<ProductInfo> list = new List<ProductInfo>();
            string strWhere = $"IsDeleted={isDeleted}";
            string cols = CreateSql.GetColsString<ProductInfo>("IsDeleted");
            SqlParameter para = null;
            if (!string.IsNullOrEmpty(keywords))
            {
                strWhere += " and (ProductName like @keywords or ProductNo like @keywords)";
                para = new SqlParameter("@keywords", $"%{keywords}%");
                list = GetModelList(strWhere, cols, para);
            }
            else
                list = GetModelList(strWhere, cols);
            return list;
        }

        /// <summary>
        /// 获取所有产品列表  绑定下拉框
        /// </summary>
        /// <returns></returns>
        public List<ProductInfo> GetAllProducts()
        {
            return GetModelList("ProductId,ProductName");
        }

        /// <summary>
        /// 获取指定商品在指定仓库中的总库存
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public int GetProductCount(int storeId,int regionId,int productId)
        {
            string sql = "select sum(ProductCount) from ProductStoreInfos where 1=1";
            if (productId > 0)
                sql += " and ProductId=" + productId;
            if (storeId > 0)
                sql += " and StoreId=" + storeId;
            if (regionId > 0)
                sql += " and SRegionId=" + regionId;
            object oCount = SqlHelper.ExecuteScalar(sql, 1);
            int reCount = 0;
            if (oCount != null && oCount.ToString() != "")
                reCount = oCount.GetInt();
            return reCount;
        }

        /// <summary>
        /// 获取指定产品信息
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public ProductInfo GetProduct(int productId)
        {
            string cols = CreateSql.GetColsString<ProductInfo>("IsDeleted");
            return GetById(productId, cols);
        }

        /// <summary>
        /// 判断产品是否已存在
        /// </summary>
        /// <param name="productName"></param>
        /// <returns></returns>
        public bool ExistsProduct(string productName)
        {
            return ExistsByName("ProductName", productName);
        }

        /// <summary>
        /// 添加产品信息
        /// </summary>
        /// <param name="proInfo"></param>
        /// <param name="isGetId"></param>
        /// <returns></returns>
        public int AddProduct(ProductInfo proInfo,int isGetId)
        {
            string cols = CreateSql.GetColsString<ProductInfo>("ProductId,IsDeleted");
            return Add(proInfo,cols, isGetId);
        }

        /// <summary>
        /// 修改产品信息
        /// </summary>
        /// <param name="proInfo"></param>
        /// <returns></returns>
        public bool UpdateProduct(ProductInfo proInfo)
        {
            string cols = CreateSql.GetColsString<ProductInfo>("IsDeleted");
            return Update(proInfo, cols);
        }
    }
}
